package com.waidi.clothespad.adapter;


import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CheckableImageButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView;

import com.waidi.clothespad.R;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
 * Created by ll on 2018/7/5.
 */

public class MyListViewAdapter extends BaseAdapter {

    private String TAG = "MyListViewAdapter";
    private Context mContext;
    private LayoutInflater mInflater;
    private LinkedList<ListviewData> mData;
    private int defaultSelection = -1;
    private Map<Integer,Boolean> map=new HashMap<>();// 存放已被选中的CheckBox
    public Map<Integer,CodeCheck> map1=new HashMap<>();// 存放已被选中的CheckBox



    public MyListViewAdapter() {}

    public MyListViewAdapter(LinkedList<ListviewData> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        //mInflater = LayoutInflater.from(mContext);
    }

    public int getSelection()
    {

        return  defaultSelection;
    }

    @Override
    public int getCount() {
        return mData.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        try {


         ViewHolder holder = null;
        if(convertView == null){

            convertView = LayoutInflater.from(mContext).inflate(R.layout.qc_list_item,parent,false);
            holder = new ViewHolder();
            holder.img_icon = (ImageView) convertView.findViewById(R.id.imgtou);
            holder.txt_content0 = (TextView) convertView.findViewById(R.id.content0);
            holder.txt_content1 = (TextView) convertView.findViewById(R.id.content1);

            holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox0);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.img_icon.setImageResource(mData.get(position).getImgId());
        holder.txt_content0.setText(mData.get(position).getContent0());
        holder.txt_content1.setText(mData.get(position).getContent1());
        holder.checkbox.setText(mData.get(position).getCode());



        //String code = getText().toString()
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true){
                    CodeCheck tmp = new CodeCheck(mData.get(position).getCode(),true);
                    Log.i(TAG,mData.get(position).getCode());
                    map1.put(position,tmp);
                }else {
                    map1.remove(position);
                }
            }
        });

        if(map1!=null&&map1.containsKey(position)){
            holder.checkbox.setChecked(true);
        }else {
            holder.checkbox.setChecked(false);
        }





        if (position == defaultSelection) {// 选中时设置单纯颜色
           // holder.txt_content0.setTextColor("0");
            //convertView.setBackgroundColor(Color.rgb(222,233,12));
        } else {// 未选中时设置selector
            //holder.txt_item.setTextColor(colors);
           // convertView.setBackgroundColor(Color.TRANSPARENT);
        }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return convertView;
    }

    private class ViewHolder
    {
        ImageView img_icon;
        TextView txt_content0;
        TextView txt_content1;
        CheckBox checkbox;
    }

    /**
     * @param position
     *            设置高亮状态的item
     */
    public void setSelectPosition(int position) {
        if (!(position < 0 || position > mData.size())) {
            defaultSelection = position;
            notifyDataSetChanged();
        }
    }


    public void clearSelectPosition(int position)
    {
        if (!(position < 0 || position > mData.size())) {
            defaultSelection = -1;
            notifyDataSetChanged();
        }
    }
    public final class CodeCheck
    {
        public String code="";
        public Boolean ischeck=false;

        public CodeCheck(String Code,Boolean ischecks)
        {
            code = Code;
            ischeck = ischecks;
        }
    }

    }